package com.ruoyi.web.controller.set;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.SetStepCode;
import com.ruoyi.system.service.ISetStepCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 步骤类型Controller
 *
 * @author ruoyi
 * @date 2022-11-21
 */
@RestController
@Api(tags = "步骤类型信息")
@RequestMapping("/set/stepCode")
public class SetStepCodeController extends BaseController
{
    @Autowired
    private ISetStepCodeService setStepCodeService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询步骤类型列表
     */
    //@PreAuthorize("@ss.hasPermi('set:stepCode:list')")
    @ApiOperation(value = "查询步骤类型列表")
    @GetMapping("/list")
    public TableDataInfo list(@ApiParam(value = "步骤类型参数", required = false) SetStepCode setStepCode)
    {
        startPage();
        List<SetStepCode> list = setStepCodeService.selectSetStepCodeList(setStepCode);
        return getDataTable(list);
    }

    /**
     * 查询步骤类型下拉接口
     */
    //@PreAuthorize("@ss.hasPermi('set:stepCode:interface')")
    @ApiOperation(value = "查询步骤类型下拉接口")
    @GetMapping("/interface")
    public AjaxResult interfaces(@ApiParam(value = "步骤类型参数", required = false) SetStepCode setStepCode)
    {

        List<SetStepCode> list = setStepCodeService.selectSetStepCodeList(setStepCode);
        return AjaxResult.success(list);
    }

    /**
     * 导出步骤类型列表
     */
    //@PreAuthorize("@ss.hasPermi('set:stepCode:export')")
    @Log(title = "步骤类型", businessType = BusinessType.EXPORT)
    @ApiOperation(value = "导出步骤类型列表")
    @GetMapping("/export")
    public AjaxResult export(HttpServletResponse response, @ApiParam(value = "步骤类型参数", required = false) SetStepCode setStepCode)
    {
        List<SetStepCode> list = setStepCodeService.selectSetStepCodeList(setStepCode);
        ExcelUtil<SetStepCode> util = new ExcelUtil<SetStepCode>(SetStepCode.class);
        return util.exportExcel( list, "步骤类型数据");
    }

    /**
     * 导入步骤类型列表
     */
    @Log(title = "步骤类型", businessType = BusinessType.IMPORT)
    //@PreAuthorize("@ss.hasPermi('set:stepCode:import')")
    @ApiOperation(value = "导入步骤类型列表")
    @PostMapping("/importData")
    public AjaxResult importData(@ApiParam(value = "导入文件", required = true) MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SetStepCode> util = new ExcelUtil<SetStepCode>(SetStepCode.class);
        List<SetStepCode> stepCodeList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = setStepCodeService.importStepCode(stepCodeList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 导入模板
     */
    @ApiOperation(value = "导入模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        List<SetStepCode> list = setStepCodeService.selectSetStepCodeList(new SetStepCode());
        ExcelUtil<SetStepCode> util = new ExcelUtil<SetStepCode>(SetStepCode.class);
        return util.importTemplateExcel(list,"步骤类型数据");
    }

    /**
     * 获取步骤类型详细信息
     */
    //@PreAuthorize("@ss.hasPermi('set:stepCode:query')")
    @ApiOperation(value = "获取步骤类型详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@ApiParam(value = "步骤类型id", required = true) @PathVariable("id") Long id)
    {
        return AjaxResult.success(setStepCodeService.selectSetStepCodeById(id));
    }

    /**
     * 新增步骤类型
     */
    //@PreAuthorize("@ss.hasPermi('set:stepCode:add')")
    @Log(title = "步骤类型", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增步骤类型")
    @PostMapping
    public AjaxResult add(@ApiParam(value = "步骤类型实体类", required = true) @RequestBody SetStepCode setStepCode)
    {
        if (UserConstants.NOT_UNIQUE.equals(setStepCodeService.checkNameUnique(setStepCode)))
        {
            return AjaxResult.error("新增步骤类型'" + setStepCode.getStepName() + "'失败，步骤类型名称已存在");
        }
        return toAjax(setStepCodeService.insertSetStepCode(setStepCode));
    }

    /**
     * 修改步骤类型
     */
    //@PreAuthorize("@ss.hasPermi('set:stepCode:edit')")
    @Log(title = "步骤类型", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改步骤类型")
    @PutMapping
    public AjaxResult edit(@ApiParam(value = "步骤类型实体类", required = true) @RequestBody SetStepCode setStepCode)
    {
        if (UserConstants.NOT_UNIQUE.equals(setStepCodeService.checkNameUnique(setStepCode)))
        {
            return AjaxResult.error("修改步骤类型'" + setStepCode.getStepName() + "'失败，步骤类型名称已存在");
        }
        return toAjax(setStepCodeService.updateSetStepCode(setStepCode));
    }

    /**
     * 删除步骤类型
     */
    //@PreAuthorize("@ss.hasPermi('set:stepCode:remove')")
    @Log(title = "步骤类型", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除步骤类型")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@ApiParam(value = "步骤类型id数组", required = true) @PathVariable Long[] ids)
    {
        return toAjax(setStepCodeService.deleteSetStepCodeByIds(ids));
    }
}
